package com.freecat.xmqgct.utils;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

/**
 * User: Luozi
 * Date: 2017-10-13
 * Content:
 */

public class SimpleRecyclerAdapter<T> extends RecyclerView.Adapter<SimpleRecyclerAdapter.ViewHolder> {

    private int mLayoutResId;
    private List<T> mDataList;
    private LayoutInflater mInflater;
    private OnBindViewListener<T> mOnBindViewListener;


    public SimpleRecyclerAdapter(int layoutResId, List<T> list) {
        mLayoutResId = layoutResId;
        mDataList = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (mInflater == null) mInflater = LayoutInflater.from(parent.getContext());
        return new ViewHolder(mInflater.inflate(mLayoutResId, parent, false), viewType);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        if (mOnBindViewListener != null)
            mOnBindViewListener.onBindViewHolder(position, mDataList.get(position), holder.getItemView());
    }

    @Override
    public int getItemCount() {
        if (mDataList!=null){
            return mDataList.size();
        }else {

            return 0;
        }

    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private View mItemView;

        ViewHolder(View itemView, int viewType) {
            super(itemView);
            mItemView = itemView;
        }

        View getItemView() {
            return mItemView;
        }

    }

    public interface OnBindViewListener<T> {
        void onBindViewHolder(int position, T t, View view);
    }

    public SimpleRecyclerAdapter<T> setOnBindViewListener(OnBindViewListener<T> l) {
        mOnBindViewListener = l;
        return this;
    }

    public SimpleRecyclerAdapter<T> setDataList(List<T> dataList) {
        mDataList.clear();
        System.out.println("dataList================"+dataList.size());
        System.out.println("mDataList================"+mDataList.size());
        mDataList = dataList;
        notifyDataSetChanged();
        return this;
    }

    public SimpleRecyclerAdapter<T> addDataList(List<T> dataList) {
        mDataList.addAll(dataList);
        notifyDataSetChanged();
        return this;
    }

    public SimpleRecyclerAdapter<T> addData(T data) {
        mDataList.add(data);
        notifyDataSetChanged();
        return this;
    }

    public SimpleRecyclerAdapter<T> setLayoutResId(int layoutResId) {
        mLayoutResId = layoutResId;
        return this;
    }

    public T getItem(int position) {
        return mDataList.get(position);
    }

    public List<T> getDataList() {
        return mDataList;
    }

    public void removeData(int position) {
        mDataList.remove(position);
        notifyDataSetChanged();
    }

    public void clearData() {
        mDataList.clear();
        notifyDataSetChanged();
    }

    public void removeData(List<T> list) {
        mDataList.removeAll(list);
        notifyDataSetChanged();
    }

    public void removeData(T bean) {
        mDataList.remove(bean);
        notifyDataSetChanged();
    }
}
